﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColorConvert
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DataBindings dataB;

        //Main Window Initialize
        public MainWindow()
        {
            InitializeComponent();
            dataB = new DataBindings { tabWidth = 93.3f, versionNumber = "v0.1" };
            this.DataContext = dataB;
            this.AboutProductTitle.Content = "ColorConvert" + " - " + dataB.versionNumber;
        }
        //Hexidecimal to RGB Decimal Controller
        public void HexToRGB(string hexColor)
        {
            hexColor = hexColor.Replace("#", "");
            if (hexColor.Length == 6)
            {
                List<string> hexParts = SplitInParts(hexColor, 2).ToList();

                RInput.Text = HexToInt(hexParts[0]).ToString();
                GInput.Text = HexToInt(hexParts[1]).ToString();
                BInput.Text = HexToInt(hexParts[2]).ToString();

                RInput2.Text = IntToDec(HexToInt(hexParts[0])).ToString();
                GInput2.Text = IntToDec(HexToInt(hexParts[1])).ToString();
                BInput2.Text = IntToDec(HexToInt(hexParts[2])).ToString();
            }
        }
        //Hexidecimal to RGB Decimal Conversion
        public int HexToInt(string hexPart)
        {
            return Convert.ToInt32(hexPart, 16);
        }
        //RGB Decimal to RGB Decimal / 255 Conversion
        public float IntToDec(int intValue)
        {
            return (float)intValue / 255;
        }
        //RGB Decimal /255 to RGB Decimal Conversion
        public int DecToInt(float decValue)
        {
            return (int)(decValue * 255);
        }
        //RGB Decimal to Hexidecimal Conversion
        public string RGBToHex(int rgbValue)
        {

            return rgbValue.ToString("X2");
        }
        //Hexidecimal Splitter
        public static IEnumerable<String> SplitInParts(String s, Int32 partLength)
        {
            if (s == null)
                throw new ArgumentNullException("s");
            if (partLength <= 0)
                throw new ArgumentException("Part length has to be positive.", "partLength");

            for (var i = 0; i < s.Length; i += partLength)
                yield return s.Substring(i, Math.Min(partLength, s.Length - i));
        }
        //Test RGB Validity & Convert
        private void RGBTest(KeyEventArgs e)
        {
            if (RInput.Text != null && GInput.Text != null && BInput.Text != null)
            {
                bool ok = true;
                int r = 0;
                int g = 0;
                int b = 0;

                int.TryParse(RInput.Text, out r);
                if (r > 255)
                {
                    ok = false;
                    RInput.Background = Brushes.Red;
                }
                else
                {
                    RInput.Background = Brushes.White;
                }
                int.TryParse(GInput.Text, out g);
                if (g > 255)
                {
                    ok = false;
                    GInput.Background = Brushes.Red;
                }
                else
                {
                    GInput.Background = Brushes.White;
                }
                int.TryParse(BInput.Text, out b);
                if (b > 255)
                {
                    ok = false;
                    BInput.Background = Brushes.Red;
                }
                else
                {
                    BInput.Background = Brushes.White;
                }
                if (e.Key == Key.Enter)
                {
                    if (ok)
                    {
                        RInput2.Text = IntToDec(r).ToString();
                        GInput2.Text = IntToDec(g).ToString();
                        BInput2.Text = IntToDec(b).ToString();
                        HexInput.Text = "#" + RGBToHex(r) + RGBToHex(g) + RGBToHex(b);
                    }
                }
            }
        }
        //Test RGB Decimal Validity & Convert
        private void RGBDecimalTest(KeyEventArgs e)
        {
            if (RInput2.Text != null && GInput2.Text != null && BInput2.Text != null)
            {
                bool ok = true;
                float r = 0;
                float g = 0;
                float b = 0;

                float.TryParse(RInput2.Text, out r);
                if (r > 1)
                {
                    ok = false;
                    RInput2.Background = Brushes.Red;
                }
                else
                {
                    RInput2.Background = Brushes.White;
                }
                float.TryParse(GInput2.Text, out g);
                if (g > 1)
                {
                    ok = false;
                    GInput2.Background = Brushes.Red;
                }
                else
                {
                    GInput2.Background = Brushes.White;
                }
                float.TryParse(BInput2.Text, out b);
                if (b > 1)
                {
                    ok = false;
                    BInput2.Background = Brushes.Red;
                }
                else
                {
                    BInput2.Background = Brushes.White;
                }
                if (e.Key == Key.Enter)
                {
                    if (ok)
                    {
                        RInput.Text = DecToInt(r).ToString();
                        GInput.Text = DecToInt(g).ToString();
                        BInput.Text = DecToInt(b).ToString();
                        HexInput.Text = "#" + DecToInt(r).ToString("X2") + DecToInt(g).ToString("X2") + DecToInt(b).ToString("X2");
                    }
                }
            }
        }
        //Hexidecimal Textbox Input
        private void HexInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                HexToRGB(HexInput.Text);
            }
        }
        //RGB (R) Textbox Input
        private void RInput_KeyDown(object sender, KeyEventArgs e)
        {
            RGBTest(e);
        }
        //RGB (G) Textbox Input
        private void GInput_KeyDown(object sender, KeyEventArgs e)
        {
            RGBTest(e);
        }
        //RGB (B) Textbox Input
        private void BInput_KeyDown(object sender, KeyEventArgs e)
        {
            RGBTest(e);
        }
        //RGB Decimal (R) Textbox Input
        private void RInput2_KeyDown(object sender, KeyEventArgs e)
        {
            RGBDecimalTest(e);
        }
        //RGB Decimal (G) Textbox Input
        private void GInput2_KeyDown(object sender, KeyEventArgs e)
        {
            RGBDecimalTest(e);
        }
        //RGB Decimal (B) Textbox Input
        private void BInput2_KeyDown(object sender, KeyEventArgs e)
        {
            RGBDecimalTest(e);
        }
        //Automatic Selection for HexInput
        private void HexInput_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for RInput
        private void RInput_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for GInput
        private void GInput_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for BInput
        private void BInput_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for RInput2
        private void RInput2_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for GInput2
        private void GInput2_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //Automatic Selection for BInput2
        private void BInput2_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }
        //On click Clear All button
        private void ClearAll_Click(object sender, RoutedEventArgs e)
        {
            HexInput.Text = "";

            RInput.Text = "";
            GInput.Text = "";
            BInput.Text = "";

            RInput2.Text = "";
            GInput2.Text = "";
            BInput2.Text = "";
        }

    }
}